package jgcp.client;

import jgcp.client.CmdClient.STATUS;
import jgcp.common.network.Connection;
import jgcp.common.network.FileReceiver;
import jgcp.message.Message;
import jgcp.message.MessageHandler;
import jgcp.message.MessageType;
/**
 * 
 * @Date 31/05/2009
 * @author Jie Zhao (288654)
 * @version 1.0
 */
public class ClientMessageHandler extends MessageHandler {
	public ClientMessageHandler(Connection con) {
		super(con);
		setDaemon(true);
	}

	public void processMessage(Message m) throws Exception {
		if (m.getType() == MessageType.LOGIN_SUCCESS) {
			CmdClient.status = STATUS.CONNECTED;
			System.out.println(m.getMessageBody());
		}
		else if(m.getType() == MessageType.LOGIN_REJECT)
		{
			CmdClient.status = STATUS.NOT_CONNECTED;
			System.out.println(m.getMessageBody());
		}
		else if(m.getType() == MessageType.GETRESULT_READY)
		{
			String hostname = connection.getSocket().getInetAddress().getHostAddress();
			int port = Integer.parseInt(m.getMessageBody());
			FileReceiver<String> fr = new FileReceiver<String>(hostname,port,CmdClient.filename);
			fr.start();
		}
		else
		{
			System.out.println(m.getMessageBody());
		}
	}
}
